package input.pilot;

import settings.ControlSettings;
import fileHandling.language.options.OptionValues;
import gameStates.absGamesStates.AbsIngameState;
import input.PlayerMouseInput;
import logic.ships.hunter.Hunter;
import logic.weapons.WeaponType;

import com.jme.input.controls.GameControl;
import com.jme.input.controls.binding.MouseAxisBinding;
import com.jme.input.controls.binding.MouseOffsetBinding;
import com.jme.input.controls.controller.ActionController;
import com.jme.input.controls.controller.ActionRepeatController;
import com.jme.input.controls.controller.Axis;
import com.jme.input.controls.controller.GameControlAction;
import com.jme.input.controls.controller.RotationController;

/**
 * 
 * 
 * @author Wasserleiche
 */
public class PilotMouseInput extends PlayerMouseInput {
	
	private GameControl rotateUp, rotateDown, rotateLeft, rotateRight;
	
	private RotationController mouseRotateLeftRight, mouseRotateUpDown;
	private ActionController stopMouse;
	private ActionRepeatController fireEnergyWeapons;
	
	private Hunter hunter;
		
	public PilotMouseInput(AbsIngameState ingameState) {
		super(ingameState.getRootNode());
		
		hunter = ingameState.getPlayer().getHunter();
		setMouseBindings();
		setMouseActions();
		hunter.addHunterFlightController(new CameraNodeController(hunter, ingameState.getCamNode()));
	}
	
	@Override
	protected void setMouseBindings() {
		super.setMouseBindings();
		rotateLeft = manager.addControl("Rotate Left");
		rotateLeft.addBinding(new MouseOffsetBinding(MouseAxisBinding.AXIS_X, true));
		rotateRight = manager.addControl("Rotate Right");
		rotateRight.addBinding(new MouseOffsetBinding(MouseAxisBinding.AXIS_X, false));
		rotateUp = manager.addControl("Rotate Up");
		rotateUp.addBinding(new MouseOffsetBinding(MouseAxisBinding.AXIS_Y, true));
		rotateDown = manager.addControl("Rotate Down");
		rotateDown.addBinding(new MouseOffsetBinding(MouseAxisBinding.AXIS_Y, false));
	}
	
	private void setMouseActions() {
		float sensitivity = hunter.getTurnSpeed();
		mouseRotateLeftRight = new RotationController(hunter, rotateLeft, rotateRight, sensitivity, Axis.Y);
		hunter.addHunterFlightController(mouseRotateLeftRight);
		
		mouseRotateUpDown = new RotationController(hunter, rotateUp, rotateDown, sensitivity, Axis.X);
		if(ControlSettings.get().getMouseInverted())
			mouseRotateUpDown = new RotationController(hunter, rotateDown, rotateUp, sensitivity, Axis.X);
		hunter.addHunterFlightController(mouseRotateUpDown);
		
		setMouseEnabled(false);
		
		GameControl stopMouseButton = getMouseButton(ControlSettings.get().getLookAround());
		stopMouse = new ActionController(stopMouseButton, new GameControlAction() {
			@Override
			public void released(GameControl control, float time) {
				setMouseEnabled(false);
				if(ControlSettings.get().getHunterRotationReset()) 
					hunter.setResetRotContorller(new ResetRotationController(hunter));
			}
			
			@Override
			public void pressed(GameControl control, float time) {
				setMouseEnabled(true);
				hunter.removeResetRotController();
			}
	    });
		hunter.addHunterFlightController(stopMouse);
		
		GameControl fireButton = getMouseButton(ControlSettings.get().getFireEnergyWeapons());
		fireEnergyWeapons = new ActionRepeatController(fireButton, 100, new Runnable() {
			@Override
			public void run() {
				if(hunter != null && hunter.getPilot().isAlive()) 
					hunter.fireWeapons(getMouseDirection(), WeaponType.Energy);
			}
	    });
		hunter.addHunterFlightController(fireEnergyWeapons);
	}
	
	protected GameControl getMouseButton(OptionValues value) {
		switch(value) {
			case Left_Mouse_Button: return leftButton;
			case Middle_Mouse_Button: return middleButton;
			case Right_Mouse_Button: return rightButton;
		}
		return null;
	}
	
	public void setMouseEnabled(boolean enabled) {
		mouseEnabled = enabled;
		rotateLeft.setEnabled(mouseEnabled);
		rotateRight.setEnabled(mouseEnabled);
		rotateUp.setEnabled(mouseEnabled);
		rotateDown.setEnabled(mouseEnabled);
		
		if(mouseEnabled) hunter.removeMoveToMissionController();
	}
	
	@Override
	public void changeControlSettings() {
		super.changeControlSettings();
		
		hunter.removeHunterFlightController(mouseRotateLeftRight);
		hunter.removeHunterFlightController(mouseRotateUpDown);
		hunter.removeHunterFlightController(fireEnergyWeapons);
		hunter.removeHunterFlightController(stopMouse);
		
		setMouseBindings();
		setMouseActions();
	}
}